// import Base64 from ''
let base64 = require("./base64.js").Base64;
/**
 * GET请求封装
 */

const Api = "https://j.kaletcg.com/lzmtest"

// const Api = "http://192.168.0.38:8081"

const imgUrl = "https://lzmordershop.oss-cn-hangzhou.aliyuncs.com";

function get(url, data = {}) {
    return request(Api + url, data, "GET");
}

function ltget(url, data = {}) {
    return request(Apia + url, data, "GET");
}

/**
 * POST请求封装
 */
function post(url, data = {}) {
    return request(Api + url, data, "POST");
}

/**
 * PUT请求封装
 */
function put(url, data = {}) {
    return request(Api + url, data, "PUT");
}

/**
 * DELETE请求封装
 */
function deletea(url, data = {}) {
    return request(Api + url, data, "DELETE");
}

function request(url, data = {}, method = "GET") {
    // wx.showLoading({
    // 	title: '加载中'
    // });
    // var token = {
    // 	token: '29-78963214'
    // }
    var obj = {
        token: uni.getStorageSync("token"),
    };
    const token = uni.getStorageSync("token");

    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            data: {
                ...data,
            },
            method: method,
            header: {
                "Content-Type": method == "GET" ?
                    "application/x-www-form-urlencoded" : "application/json",
                "api-access-token": token,
                // 'Content-Type': method == 'GET' ? 'application/json' :
                // 	'application/x-www-form-urlencoded',
            },
            success: (res) => {
                if (res.statusCode == 200) {
                    resolve(res);
                    if (res.data.code == 500) {
                        uni.showToast({
                            title: res.data.message,
                            icon: "none",
                        });
                    }
                    if (res.data.code == 555) {
                        uni.removeStorageSync("token");
                        uni.removeStorageSync("userInfo");
                        uni.showModal({
                            content: "请登录",
                            success: function (res) {
                                if (res.confirm) {
                                    uni.navigateTo({
                                        url: "/pages/login/login",
                                    });
                                } else if (res.cancel) {
                                    uni.navigateBack({});
                                }
                            },
                        });
                    }
                } else {
                    //请求失败
                    reject("请求失败：" + res.statusCode);
                }
                // wx.hideLoading()
            },
            fail: (err) => {
                console.log(err);
                uni.showToast({
                    title: "服务器连接异常，请检查网络再试",
                    icon: "none",
                });
                reject("服务器连接异常，请检查网络再试");
                // wx.hideLoading();
            },
        });
    });
}

module.exports = {
    request,
    get,
    post,
    put,
    deletea,
    imgUrl,
    Api,
    ltget,
};